package com.ssy.lingxi.platform.template.service.serviceimpl.mobile;

import cn.hutool.core.collection.CollectionUtil;
import com.ssy.lingxi.common.constant.template.PageTemplateTypeEnum;
import com.ssy.lingxi.common.response.ResponseCode;
import com.ssy.lingxi.common.response.Wrapper;
import com.ssy.lingxi.platform.template.service.constant.CommonBooleanEnum;
import com.ssy.lingxi.platform.template.service.entity.adorn.AppAdorn;
import com.ssy.lingxi.platform.template.service.entity.adorn.PageTemplate;
import com.ssy.lingxi.platform.template.service.model.dto.common.MemberIdRoleIdShopIdDTO;
import com.ssy.lingxi.platform.template.service.model.dto.common.SiteIdAndShopIdDTO;
import com.ssy.lingxi.platform.template.service.repository.AppAdornRepository;
import com.ssy.lingxi.platform.template.service.repository.PageTemplateRepository;
import com.ssy.lingxi.platform.template.service.service.mobile.PageTemplateMobileService;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @menu mobile - 页面模板 - 业务实现层
 * @author fhj
 * @version 2.0.0
 * @date 2020/12/11
 */
@Slf4j
@Service
public class PageTemplateMobileServiceImpl implements PageTemplateMobileService {

    @Resource
    private PageTemplateRepository pageTemplateRepository;
    @Resource
    private AppAdornRepository appAdornRepository;

    @Override
    @ApiOperation(value = "获取APP企业商城模板B端装修")
    public Wrapper<AppAdorn> findAppEnterpriseTemplateB(SiteIdAndShopIdDTO dto) {
        PageTemplate pageTemplate = pageTemplateRepository.findByTypeAndStatusAndSiteIdAndShopId(
                PageTemplateTypeEnum.MALL.getCode(),
                CommonBooleanEnum.YES.getCode(),
                dto.getSiteId(),
                dto.getShopId()
        );
        if(pageTemplate == null) {
            return Wrapper.fail(ResponseCode.PT_NOT_FOUND_TEMPLATE);
        }

        return Wrapper.success(appAdornRepository.findByTemplateId(pageTemplate.getId()));
    }

    @Override
    @ApiOperation(value = "获取APP企业商城模板C端装修")
    public Wrapper<AppAdorn> findAppEnterpriseTemplateC(SiteIdAndShopIdDTO dto) {
        PageTemplate pageTemplate = pageTemplateRepository.findByTypeAndStatusAndSiteIdAndShopId(
                PageTemplateTypeEnum.MALL.getCode(),
                CommonBooleanEnum.YES.getCode(),
                dto.getSiteId(),
                dto.getShopId()
        );
        if(pageTemplate == null) {
            return Wrapper.fail(ResponseCode.PT_NOT_FOUND_TEMPLATE);
        }

        return Wrapper.success(appAdornRepository.findByTemplateId(pageTemplate.getId()));
    }

    @Override
    @ApiOperation(value = "获取APP自营商城模板装修")
    public Wrapper<AppAdorn> findAppSelfTemplate(MemberIdRoleIdShopIdDTO dto) {
        //封装正在使用的自营商城模板装修内容
        List<PageTemplate> pageTemplateList = pageTemplateRepository.findByTypeAndStatusAndShopId(
                PageTemplateTypeEnum.SELF.getCode(),
                CommonBooleanEnum.YES.getCode(),
                dto.getShopId()
        );
        if(CollectionUtil.isNotEmpty(pageTemplateList)) {
            pageTemplateList = pageTemplateList.stream().filter(a ->
                    CollectionUtil.isNotEmpty(a.getUseSelfTemplate()) &&
                            a.getUseSelfTemplate().stream().anyMatch(b ->
                                    b.getMemberId().equals(dto.getMemberId()) && b.getRoleId().equals(dto.getRoleId())
                            )
            ).collect(Collectors.toList());

            if(CollectionUtil.isNotEmpty(pageTemplateList)) {
                if (pageTemplateList.size() > 1) {
                    log.error("正在使用的自营商城模板出现多个【检查原因】");
                }
                PageTemplate pageTemplate = pageTemplateList.get(0);

                AppAdorn appAdorn = appAdornRepository.findByTemplateIdAndMemberIdAndRoleId(
                        pageTemplate.getId(),
                        dto.getMemberId(),
                        dto.getRoleId()
                );
                return Wrapper.success(appAdorn);
            }
        }

        return Wrapper.success(null);
    }

    @Override
    @ApiOperation(value = "获取APP渠道商城模板装修")
    public Wrapper<AppAdorn> findAppChannelTemplate(MemberIdRoleIdShopIdDTO dto) {
        //封装正在使用的渠道或渠道自有模板装修内容
        List<PageTemplate> pageTemplateList = pageTemplateRepository.findByTypeAndStatusAndShelfAndShopId(
                PageTemplateTypeEnum.CHANNEL.getCode(),
                CommonBooleanEnum.YES.getCode(),
                CommonBooleanEnum.YES.getCode(),
                dto.getShopId()
        );
        if(CollectionUtil.isNotEmpty(pageTemplateList)) {
            pageTemplateList = pageTemplateList.stream().filter(a ->
                    CollectionUtil.isNotEmpty(a.getUseChannelTemplate()) &&
                            a.getUseChannelTemplate().stream().anyMatch(b ->
                                    b.getMemberId().equals(dto.getMemberId()) && b.getRoleId().equals(dto.getRoleId())
                            )
            ).collect(Collectors.toList());

            //渠道模板和渠道自有模板
            if(CollectionUtil.isNotEmpty(pageTemplateList)) {
                if (pageTemplateList.size() > 1) {
                    log.error("正在使用的渠道模板出现多个【检查原因】");
                }
                PageTemplate pageTemplate = pageTemplateList.get(0);

                AppAdorn appAdorn = appAdornRepository.findByTemplateIdAndMemberIdAndRoleId(
                        pageTemplate.getId(),
                        dto.getMemberId(),
                        dto.getRoleId()
                );

                return Wrapper.success(appAdorn);
            }
        }

        return Wrapper.fail(ResponseCode.PT_CAN_T_GET_CHANNEL_TEMPLATE_DECORATION_CONTENT);
    }
}
